package sns.service;

import java.util.LinkedList;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import sns.bean.Mblog;
import sns.bean.User;
import sns.dao.MblogDao;
import sns.dao.RelationDao;

@Transactional
public class MblogService {
	
	private MblogDao mblogDao;
	private RelationDao relationDao;
	
	public void setRelationDao(RelationDao relationDao) {
		this.relationDao = relationDao;
	}

	/**
	 * get mblog by its id
	 * 
	 * @param idmblog
	 * @return
	 */
	public Mblog getMblogById(Long idmblog){
		return mblogDao.getMblogById(idmblog);
	}
	
	/**
	 * get the recent n mblogs of all users
	 * 
	 * @param n
	 * @return
	 */
	public List<Mblog> getAllMblogs(int n){
		return mblogDao.getAllMblogs(n);
	}
	
	/**
	 * get the followees’ mblogs(idols' mblogs) and the user's own mblogs
	 * 
	 * @param user
	 * @return
	 */
	public List<Mblog> getFolloweeMblogs(User user){
		List<User> users = relationDao.getFollowees(user);
		users.add(user);
		if(users.size()==0){
			return new LinkedList<Mblog>();
		}else{
			return mblogDao.getListUsersMblogs(users);
		}
	}
	
	/**
	 * get mblogs of the user
	 * 
	 * @param user
	 * @return
	 */
	public List<Mblog> getUserMblogs(User user){
		return mblogDao.getUserMblogs(user);
	}
	
	/**
	 * get the number of mblogs of this user
	 * 
	 * @param user
	 * @return
	 */
	public int getMblogNum(User user){
		return mblogDao.getMblogNum(user);
	}

	public void setMblogDao(MblogDao mblogDao) {
		this.mblogDao = mblogDao;
	}
	
	
}
